<?php

require_once('functions.php');
$member_id	= $session->get("member_id");

// Update top players.
$template->assign("players",get_top_players());

// Member is registered.
if($member_id)
{
	redirect("index.php");
}

// Assign title page.
$template->assign("title", _LB_TITLE_REGISTER);

// Get data from register form.
$data = $_REQUEST;
$message_error = array();
if(isset($data['form_id']))
{
	$sess_form_id = $session->get('form_id');
	if($sess_form_id && $sess_form_id != $data['form_id'])
	{
		redirect("index.php");
	}
	$session->set('form_id', "");
	// Check blank fields
	if(	empty($data['username']) || 
		empty($data['password']) ||
		empty($data['password2']) ||
		empty($data['email']) ||
		empty($data['fullname']) ||
		empty($data['indentify']) ||
		empty($data['address']) ||
		empty($data['phone']))
	{
		$message_error[] = _ERR_FILL_ALL_REQUIRED_FIELD;
	}
	
	// Check length username.
	if(strlen($data['username']) > 20)
	{
		$message_error[] = _ERR_MAX_LENGTH_USERNAME;
	}
	
	// Check length fullname.
	if(strlen($data['fullname']) > 50)
	{
		$message_error[] = _ERR_MAX_LENGTH_FULLNAME;
	}

	// Check retype-password not match.
	if($data['password'] != $data['password2'])
	{
		$message_error[] = _ERR_REPEAT_PASSWORD_NOT_MATCH;
	}
	
	// Check valid email
	if(!validEmail($data['email']))
	{
		$message_error[] = _ERR_INVALID_EMAIL;
	}
	
	// Check exist member
	$database->set_query("SELECT COUNT(id) FROM member WHERE username = '{$data['username']}'");
	$exist_member = $database->get_result();
	if($exist_member)
	{
		$message_error[] = _ERR_USERNAME_IS_EXISTED;
	}
	
	// Check exist member email
	$database->set_query("SELECT COUNT(id) FROM member WHERE email = '{$data['email']}'");
	$exist_email = $database->get_result();
	if($exist_email)
	{
		$message_error[] = _ERR_EMAIL_IN_USE;
	}
	
	// Check valid birthday
	if($data['birthday_year'] == -1 || $data['birthday_year'] == -1 || $data['birthday_year'] == -1)
	{
		$message_error[] = _ERR_INVALID_DATE_OF_BIRTH;
	}

	// All data are valid.
	if(count($message_error) == 0)
	{
		// Process birthday
		$birthday = $data['birthday_year'] . "-" . $data['birthday_month'] . "-" . $data['birthday_day'];
		
		// Prepare insert
		$insert_sql = "INSERT INTO member (id, username, password, fullname, email, address, indentify, phone, sex, birthday) VALUES 
						(%s, '%s', MD5('%s'), '%s', '%s', '%s', '%s', '%s', '%s', '%s')";
		$insert_sql = sprintf($insert_sql,'NULL', $data['username'], $data['password'], $data['fullname'], $data['email'], $data['address'], $data['indentify'], $data['phone'], $data['sex'], $birthday);

		if($database->query($insert_sql))
		{
			$database->set_query("SELECT * FROM member WHERE id = " . $database->insertid());
			$member = $database->get_object();
			$session->set("member_id", $member->id);
			$session->set("member_fullname", !empty($member->fullname) ? $member->fullname : $member->username);
			
			$name		= !empty($member->fullname) ? $member->fullname : $member->username;
			$password	= $data['password'];
			$to			= $data['email'];
			$subject	= "Register new member at tuoitrenews!";
			$body		= _LB_DEAR . ", {$name}\r\n\n"._LB_YOUR_NEW_INFORMATION.": \r\n 
							"._LB_USERNAME.": {$member->username}\r\n 
							"._LB_PASSWORD.": {$password}\r\n
							"._LB_NAME.": {$member->fullname}\r\n
							"._LB_CARD_NO.": {$member->indentify}\r\n 
							"._LB_ADDRESS.": {$member->address}\r\n 
							"._LB_PHONE_NO.": {$member->phone}\r\n\n  
							"._LB_THANKS.",\r\n"._LB_SUPPORT_TEAM.".";
			mail_utf8($to, $subject, $body);
			redirect("index.php");
		}
	}
	else
	{
		$form_id = uniqid();		
		$session->set('form_id', $form_id);
		$template->assign("form_id", $form_id);
		$template->assign("username", $data['username']);
		$template->assign("email", $data['email']);
		$template->assign("fullname", $data['fullname']);
		$template->assign("birthday_day", $data['birthday_day']);
		$template->assign("birthday_month", $data['birthday_month']);
		$template->assign("birthday_year", $data['birthday_year']);
		$template->assign("sex", $data['sex']);
		$template->assign("indentify", $data['indentify']);
		$template->assign("address", $data['address']);
		$template->assign("phone", $data['phone']);
	}
}
else
{
	$form_id = uniqid();
	$template->assign("form_id", $form_id);
	$session->set('form_id', $form_id);
}
$template->assign("message", implode("<br>", $message_error));
$template->display("register.tpl");
?>
